using UnityEngine;
using System.Collections;

public class GemClass : MonoBehaviour {

	// Use this for initialization
	private OTSprite me;
	private globalVarsClass gv;
	private bool pull = false;
	private OTSprite homeLock;
	private bool locked = false;
	public string gemType = "red";
	//public int checker = 0;
	void Start () {
    	gv = GameObject.Find("GlobalVariables").GetComponent<globalVarsClass>();
	}
	
	// Update is called once per frame
	void Update () {
		me = this.GetComponent<OTSprite>();
		//if(!locked){
			//me = this.GetComponent<OTSprite>();
			if(Input.GetMouseButtonDown(0)){
				if(OT.view.mouseWorldPosition.x < me.position.x+me.rect.width/2 &&
					OT.view.mouseWorldPosition.x > me.position.x-me.rect.width/2 &&
					OT.view.mouseWorldPosition.y < me.position.y+me.rect.height/2 &&
					OT.view.mouseWorldPosition.y > me.position.y-me.rect.height/2 &&
					gv.gemIsDragging == false){
					gv.gemIsDragging = true;
					pull = true;
					if(locked){
						homeLock.GetComponent<lockClass>().gemPulledOut();
						locked = false;
					}
				}else{
					pull = false;
				}
			}//else if(Input.GetMouseButton(0)){
				
		//	}
			if(Input.GetMouseButton(0) && pull){
				me.position = OT.view.mouseWorldPosition;
				/*for(int i = 0; i < gv.locksList.Count; i++){
					if(Mathf.Abs(gv.locksList[i].position.x - me.position.x) < me.rect.width/2 + gv.locksList[i].rect.width/2 &&
						Mathf.Abs(gv.locksList[i].position.y - me.position.y) < me.rect.height/2 + gv.locksList[i].rect.height/2 &&
						gv.locksList[i].GetComponent<lockClass>().full == false){
						me.position = gv.locksList[i].position;
						locked = true;
						gv.locksList[i].GetComponent<lockClass>().fillWith(gemType);
					}
				}*/
			}
			if(Input.GetMouseButtonUp(0) && pull){
				gv.gemIsDragging = false;
				for(int i = 0; i < gv.locksList.Count; i++){
					if(Mathf.Abs(gv.locksList[i].position.x - me.position.x) < me.rect.width/2 + gv.locksList[i].rect.width/2 &&
						Mathf.Abs(gv.locksList[i].position.y - me.position.y) < me.rect.height/2 + gv.locksList[i].rect.height/2 &&
						gv.locksList[i].GetComponent<lockClass>().full == false){
						me.position = gv.locksList[i].position;
						locked = true;
						homeLock = gv.locksList[i];
						gv.locksList[i].GetComponent<lockClass>().fillWith(gemType);
					}
				}
			}
		//}
		
	}
	void OnMouseDrag(){
		///checker += 1;
		//me.RotateTowards(OT.view.mouseWorldPosition);
		//me.position = me.position + me.yVector * 0.2f;
	}
}
